package com.mye.prometheusexportdcim.configuration;

import com.mye.prometheusexportdcim.model.exporter.ExporterEnv;
import com.mye.prometheusexportdcim.utils.GlobalConstant;
import com.mye.prometheusexportdcim.utils.LogLocalityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.util.StringUtils;

/**
 * @author hch
 * @date 2022年04月28日 14:41
 *
 * 获取环境变量
 */
@Configuration
@Slf4j
public class ExporterConfiguration {


    @Bean
    @Primary
    public ExporterEnv init() throws Exception {
        String exporterIdStr = System.getenv(GlobalConstant.EXPORTERID);
       // exporterIdStr = "1";
        if (StringUtils.isEmpty(exporterIdStr)) {
            throw new Exception("env " + GlobalConstant.EXPORTERID + " do not set !");
        }
        int exporterId;
        try {
            exporterId = Integer.parseInt(exporterIdStr);
            if (exporterId < 1) {
                throw new Exception("env " + GlobalConstant.EXPORTERID + " invalid! ");
            }
        } catch (Exception e) {
            throw new Exception("env EXPORTERID invalid! ");
        }

        String dcimCollectUrl = System.getenv(GlobalConstant.DCIM_COLLECT_URL);
       // dcimCollectUrl = "127.0.0.1";
        if (StringUtils.isEmpty(dcimCollectUrl)) {
            throw new Exception("env " + GlobalConstant.DCIM_COLLECT_URL + " do not set!");
        }


        ExporterEnv exporterEnv = new ExporterEnv();
        exporterEnv.setExporterId(exporterId);
        exporterEnv.setDcimCollectUrl(dcimCollectUrl);

        log.info(LogLocalityUtil.getLogLocality() + " exporterEnv: " + exporterEnv.toString());
        return exporterEnv;

    }


}
